import { createRouter, createWebHistory } from "vue-router";
import HomePage from "@/components/HomePage.vue";
import ShopPage from "@/components/ShopPage.vue";
import ShopCar from "@/components/ShopCar.vue";
import DepartmentPage from "@/components/DepartmentPage.vue";
import HealthBaike from "@/components/HealthBaike.vue";
import SearchDrugs from "@/components/SearchDrugs.vue";
import OrderPage from "@/components/OrderPage.vue";
import HomeView from "@/components/HomeView.vue";
import AiServe from "@/components/AiServe.vue";
import MyInformation from "@/components/MyInformation.vue";
import HealthInfo from "@/components/HealthInfo.vue";

const routes = [
    {
        path: "/",
        redirect: { name: "login" },
    },
    {
        path: "/loginView",
        name: "login",
        component: () => import("@/view/login/loginView.vue"),
        meta: { requiresAuth: false },
    },
    {
        path: "/registerView",
        name: "register",
        component: () => import("@/view/login/registerView.vue"),
        meta: { requiresAuth: false },
    },
    {
        path: "/HomePage",
        name: "Home",
        component: HomePage,
        meta: { requiresAuth: true },
        children: [
            {
                path: "shop",
                name: "shop",
                component: ShopPage,
                meta: { requiresAuth: true },
            },
            {
                path: "AiServe",
                name: "AiServe",
                component: AiServe,
                meta: { requiresAuth: true },
            },
            {
                path: "HomeView",
                name: "HomeView",
                component: HomeView,
                meta: { requiresAuth: true },
            },

            {
                path: "OrderPage",
                name: "OrderPage",
                component: OrderPage,
                meta: { requiresAuth: true },
            },
            {
                path: "SearchDrugs",
                name: "SearchDrugs",
                component: SearchDrugs,
                meta: { requiresAuth: true },
            },
            {
                path: "shopcar",
                name: "shopcar",
                component: ShopCar,
                meta: { requiresAuth: true },
            },
            {
                path: "baike",
                name: "baike",
                component:HealthBaike,
                meta: { requiresAuth: true },
            },
            {
                path: "CommentView",
                name: "CommentView",
                component: () => import("@/components/CommentView.vue"),
                meta: { requiresAuth: true },
            },
            {
                path: "CommentByRelease",
                name: "CommentByRelease",
                component: () => import("@/components/CommentByRelease.vue"),
                meta: { requiresAuth: true },
            },
            {
                path: "CommentByReply",
                name: "CommentByReply",
                component: () => import("@/components/CommentByReply.vue"),
                meta: { requiresAuth: true },
            },
            {
                path: "department",
                name: "department",
                component: DepartmentPage,
                props: true,
                meta: { requiresAuth: true },
            },
            {
                path: "information",
                name: "information",
                component: MyInformation,
                props: true,
                meta: { requiresAuth: true },
            },
            {
                path: "healthinfo",
                name: "healthinfo",
                component: HealthInfo,
                props: true,
                meta: { requiresAuth: true },
            },
        ],
    },
];

const router = createRouter({
    history: createWebHistory(process.env.BASE_URL),
    routes,
});

router.beforeEach((to, from, next) => {
    // 检查路由是否需要认证
    if (to.matched.some(record => record.meta.requiresAuth)) {

        const isAuthenticated = localStorage.getItem("sessionToken");
        if (!isAuthenticated) {
            // 如果用户未认证，重定向到登录页面
            next({ name: "login" });
        } else {
            // 如果用户已认证，允许访问
            next();
        }
    } else {
        // 如果路由不需要认证，直接允许访问
        next();
    }
});

export default router;